package com.umeox.ota.mvvm.viewModel;

import android.app.Application;

import androidx.databinding.ObservableBoolean;
import androidx.databinding.ObservableField;

import com.abupdate.fotainject.FotaInject;
import com.abupdate.fotainject.FotaInjector;
import com.abupdate.iot_libs.data.remote.VersionInfo;
import com.abupdate.sdk_annotation.Inject;
import com.abupdate.trace.Trace;
import com.umeox.ota.R;
import com.umeox.ota.mvvm.BaseViewModel;
import com.umeox.ota.mvvm.model.MainModel;
import com.umeox.ota.ui.activity.NoticeActivity;
import com.umeox.ota.utils.ActivityCollector;
import com.umeox.ota.utils.NotifyUtils;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers;

/**
 * @author fighter_lee
 * @date 2018/9/4
 */
public class NoticeViewModel extends BaseViewModel<MainModel> {

    private static final String TAG = "NoticeViewModel";

    @Inject
    VersionInfo versionInfo;

    public final ObservableField<String> tipsContent = new ObservableField<>();
    public final ObservableBoolean isForce = new ObservableBoolean();
    public final ObservableBoolean isChooseBtn = new ObservableBoolean();
    public final ObservableField<String> releaseNote = new ObservableField<>();
    private int mNoticeType;
    private FotaInject inject;

    public NoticeViewModel(Application application, MainModel model) {
        super(application, model);
    }

    public void init() {
        if (versionInfo != null) {
            releaseNote.set(versionInfo.getReleaseNoteByCurrentLanguage());
        }
        switch (mNoticeType) {
            case NoticeActivity.NOTICE_TYPE_SHOW_NEW_VERSION:
                isForce.set(false);
                isChooseBtn.set(true);
                tipsContent.set(getApplication().getString(R.string.notify_tips));
                break;

            case NoticeActivity.NOTICE_TYPE_SHOW_WILL_INSTALL_FORCE:
                isForce.set(true);
                String message = getApplication().getString(R.string.tips_update_prompt);
                Observable.intervalRange(1, NotifyUtils.FORCE_INSTALL_SHOW_TIME, 0, 1 * 1000, TimeUnit.MILLISECONDS)
                        .subscribeOn(Schedulers.io())
                        .subscribe(aLong -> {
                            String showMsg = message + " " + (NotifyUtils.FORCE_INSTALL_SHOW_TIME - aLong);
                            tipsContent.set(showMsg);
                            if ((NotifyUtils.FORCE_INSTALL_SHOW_TIME - aLong) <= 0) {
                                ActivityCollector.removeAllActivity();
                            }
                        });
                break;

            case NoticeActivity.NOTICE_TYPE_SHOW_DOWNLOAD_FINISH:
                isForce.set(false);
                isChooseBtn.set(true);
                tipsContent.set(getApplication().getString(R.string.tips_update_normal));
                break;

            case NoticeActivity.NOTICE_TYPE_SHOW_INSTALL_SUCCESS:
                isForce.set(false);
                isChooseBtn.set(false);
                tipsContent.set(getApplication().getString(R.string.update_success));
                break;

            default:
                Trace.e(TAG, "状态异常");
                ActivityCollector.removeAllActivity();
                break;

        }
    }

    @Override
    public void onStart() {
        Trace.d(TAG, "onStart() ====================");
        if (inject == null) {
            inject = FotaInjector.inject(this);
        }
        init();
    }

    @Override
    public void onDestroy() {
        Trace.d(TAG, "onDestroy() -----------------------");
        if (inject != null) {
            inject.unInject();
        }
    }

    public void setNoticeType(int noticeType) {
        this.mNoticeType = noticeType;
    }
}
